﻿using UnityEngine;
using NPBehave;
using NPBehave.Unity;

public class NPBehaveExampleStopsOnChange : MonoBehaviour
{

    public bool DefaultBool = false;
    public Stops StopsType;
    private Root behaviorTree;

    private int index = 0;

    void Start()
    {
        behaviorTree = new Root(
            //new Service(5f, () => { behaviorTree.Blackboard["foo"] = !behaviorTree.Blackboard.Get<bool>("foo"); },
            new Selector(
                new Selector(

                    new Failer(
                        new Action(() => Debug.Log($"loop :{index++}"))
                    ),

                    new BlackboardCondition("foo", Operator.IS_EQUAL, true, StopsType,
                        new Sequence("Sequence1-0",
                            new Action(() => Debug.LogError("foo"))
                            , new WaitUntilStopped(false)
                        )
                    ),

                    new Sequence("Sequence1-1",
                        new Action(() => Debug.Log("bar 1-1"))
                        , new WaitUntilStopped(true)
                    ),

                    new Sequence("Sequence1-2",
                        new Action(() => Debug.Log("bar 1-2"))
                        , new WaitUntilStopped(false)
                    )
                )
                , new Selector(

                    new Sequence("Sequence2-1",
                        new Action(() => Debug.Log("bar 2-1"))
                        , new WaitUntilStopped()
                    )
                )
                )
                
            //)
        );

        behaviorTree.Blackboard.Set("foo", DefaultBool);
        behaviorTree.Start();

#if UNITY_EDITOR
        Debugger debugger = (Debugger)this.gameObject.AddComponent(typeof(Debugger));
        debugger.BehaviorTree = behaviorTree;
#endif
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.T))
        {
            behaviorTree.Blackboard["foo"] = true;
        }

        if (Input.GetKeyDown(KeyCode.F))
        {
            behaviorTree.Blackboard["foo"] = false;
        }
    }

    private void OnGUI()
    {
        if (GUILayout.Button("true",GUILayout.Width(200),GUILayout.Height(100)))
        {
            behaviorTree.Blackboard["foo"] = true;
        }

        if (GUILayout.Button("false", GUILayout.Width(200), GUILayout.Height(100)))
        {
            behaviorTree.Blackboard["foo"] = false;
        }
    }

}
